home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 261 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: teal.csn.net!not-for-mail
  2. From: thads@csn.net (Thad Smith)
  3. Newsgroups: comp.lang.c,comp.os.ms-windows.nt.misc,comp.programming,comp.std.c,comp.unix.aix
  4. Subject: Re: function pointers
  5. Date: 31 Jan 1996 22:34:08 -0700
  6. Organization: T3 Systems
  7. Message-ID: <sXEExQ9ytBGL084yn@csn.net>
  8. References: <4eogio$gt0@giga.bga.com> <4eohgr$gt0@giga.bga.com>
  9. Reply-To: ThadSmith@acm.org
  10. NNTP-Posting-Host: 199.117.27.22
  11.  
  12. In article <4eohgr$gt0@giga.bga.com>, makuch@bga.com (Michael Makuch) wrote:
  13. >In article <4eogio$gt0@giga.bga.com>, makuch@bga.com says...
  14. >>
  15. >>The following c code segment compiles and works on
  16. >>NT MSVC++ and on SVR4 C compiler, but errors out
  17. >>on AIX with a type mismatch;
  18. >>
  19. >>struct foostruct1 * myfoo1();
  20. >>struct foostruct2 * myfoo2();
  21. >>void *(*ptr)();
  22. >>
  23. >>ptr = myfoo1;
  24. >>[snip]
  25. >>ptr = myfoo2;
  26. >>
  27. >>I'm passing the function pointer ptr, to a function
  28. >>which then calls myfoo1, myfoo2, etc., etc. I can think
  29. >>of several work arounds but I'd rather get the AIX
  30. >>compiler to accept it. Is there a portable solution to
  31. >>get the AIX compiler to accept it? 
  32.  
  33. If void* and struct pointers use the same format and are treated
  34. identically as function return values, you can use the following
  35. non-portable code:
  36.  
  37.     ptr = (void*(*)()) myfoo1;
  38.  
  39. That casts myfoo1 to the same type as ptr.
  40.  
  41. For portable code, rewrite myfoo1() and myfoo2() to return void*, then
  42. cast them on invocation to appropriate type.  That allows the compiler
  43. to perform any conversion needed between different pointer types.
  44. Otherwise the code using the pointer doesn't know to handle a struct
  45. pointer instead of a void*, assuming the representations are
  46. different.
  47.  
  48. Thad
  49.